#coding: utf-8
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.

# 拓展django的user表为我的用户表Users，可以增加例如身份证，电话号码，生日等字段
class Users(AbstractUser):
    gender = (('male', '男'), ('female', '女'))
    utype = (('1', '教师'), ('2', '学生'), ('3', '管理员'))
    sex = models.CharField('性别',max_length=32, choices=gender, default='男')
    usertype = models.CharField('用户类别',max_length=32, choices=utype)
    image = models.ImageField('用户头像',max_length=200,upload_to='user_images/%Y/%m/%d' ,default='default.png')
    au = models.IntegerField('用户活跃度',default=0)
    topic_num = models.IntegerField('文章数',default=0)
    visit_num = models.IntegerField('访问量',default=0)
    comment_num = models.IntegerField('总评论数',default=0)
    class Meta:
        db_table = 'users'
        verbose_name = '用户'
        verbose_name_plural = verbose_name

    def set_topic_num(self):
        self.topic_num += 1
        self.au = self.get_au()
        self.save(update_fields=['topic_num','au'])

    def set_visit_num(self):
        self.visit_num += 1
        self.au = self.get_au()
        self.save(update_fields=['visit_num','au'])

    def set_comment_num(self):
        self.comment_num += 1
        self.au = self.get_au()
        self.save(update_fields=['comment_num','au'])

    def get_au(self):
        self.au = self.topic_num * 5 + self.visit_num * 1 + self.comment_num * 1
        return self.au

    def __str__(self):
        return self.username

